﻿using System;
using System.Collections.Generic;
using System.Text;
using FDSimpleModelGenerator.Entities;
using FDSimpleModelGenerator.IRepositories;
using FDSimpleModelGenerator.IServices;

namespace FDSimpleModelGenerator.Services
{
    /// <summary>
    /// 用户服务
    /// </summary>
    public class SysUserService : BasicService<SysUser>, ISysUserService
    {
        public SysUserService(IBasicRepository<SysUser> basicRepository) : base(basicRepository)
        {
        }

        public bool IsValidLogin(string userName, string password, out int id)
        {
            var sql = "SELECT id FROM sys_user WHERE userName=@userName AND password=@password LIMIT 1";
            id = Repository<ISysUserRepository>().ExecutScalar<int>(sql, new { userName, password });
            return id > 0;
        }

        public bool IsExists(string userName)
        {
            var sql = "SELECT id FROM sys_user WHERE userName=@userName LIMIT 1";
            return Repository<ISysUserRepository>().ExecutScalar<int>(sql, new { userName }) > 0;
        }

        public int GetId(string userName)
        {
            var sql = "SELECT id from sys_user WHERE userName=@userName";
            return Repository<ISysUserRepository>().ExecutScalar<int>(sql, new { userName });
        }
    }
}
